/**
 * 
 */
package com.hfjyz.registration.service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.BindException;
import org.springframework.validation.Errors;

import com.hfjyz.registration.command.AnnouncementEditCommand;
import com.hfjyz.registration.command.AnnouncementListCommand;
import com.hfjyz.registration.contants.ExamContants;
import com.hfjyz.registration.dao.ExamDao;
import com.hfjyz.registration.model.Exam;
import com.hfjyz.registration.util.MasterUtil;

/**
 *
 */
public class AnnouncementServiceImpl extends AbstractServiceImpl implements
		AnnouncementService {
	private ExamDao examDao;

	@SuppressWarnings("rawtypes")
	@Override
	public Map initEdit(AnnouncementEditCommand cmd, Errors errors)
			throws Exception {
		if (cmd != null && cmd.getId() != null) {
			Exam exam = examDao.get(cmd.getId());
			if (exam != null) {
				SimpleDateFormat tsf = new SimpleDateFormat(
						ExamContants.YYYY_MM_DD_HH_MM);
				SimpleDateFormat dsf = new SimpleDateFormat(
						ExamContants.YYYY_MM_DD);
				cmd.setId(exam.getId());
				cmd.setName(exam.getName());
				cmd.setContents(exam.getContents());
				cmd.setExamName(exam.getExamName());
				cmd.setExamPosition(exam.getExamPosition());
				cmd.setExamTime(exam.getExamTime());
				cmd.setOnlinePay(exam.isOnlinePay());
				cmd.setRegFee(exam.getRegFee() == null ? null : String
						.valueOf(exam.getRegFee()));
				cmd.setPublishDate(format(exam.getPublishDate(), tsf));
				cmd.setAdmissionPrintEnd(format(exam.getAdmissionPrintEnd(),
						tsf));
				cmd.setAdmissionPrintStart(format(
						exam.getAdmissionPrintStart(), tsf));
				cmd.setApplyBeginDate(format(exam.getApplyBeginDate(), tsf));
				cmd.setApplyDeadDate(format(exam.getApplyDeadDate(), tsf));
				cmd.setExamDate(format(exam.getExamDate(), dsf));
				cmd.setReApplyEnd(format(exam.getReApplyEnd(), tsf));
				cmd.setReApplyStart(format(exam.getReApplyStart(), tsf));
				cmd.setScorePublishDate(format(exam.getScorePublishDate(), tsf));
				cmd.setPayStartDate(format(exam.getPayStartDate(), tsf));
				cmd.setPayEndDate(format(exam.getPayEndDate(), tsf));
			}
		}
		return null;
	}

	private String format(Date d, SimpleDateFormat sdf) {
		if (d == null) {
			return null;
		}
		return sdf.format(d);
	}

	private Date parse(String d, SimpleDateFormat sdf) {
		if (d == null || d.length() == 0) {
			return null;
		}
		try {
			return sdf.parse(d);
		} catch (ParseException e) {
			return null;
		}
	}

	@Transactional
	@Override
	public void editOk(HttpServletRequest request, AnnouncementEditCommand cmd,
			BindException errors) throws Exception {

		Exam exam = new Exam();
		SimpleDateFormat tsf = new SimpleDateFormat(
				ExamContants.YYYY_MM_DD_HH_MM);
		SimpleDateFormat dsf = new SimpleDateFormat(ExamContants.YYYY_MM_DD);
		exam.setId(cmd.getId());
		exam.setName(cmd.getName());
		exam.setContents(cmd.getContents());
		exam.setExamName(cmd.getExamName());
		exam.setExamTime(cmd.getExamTime());
		exam.setExamPosition(cmd.getExamPosition());
		exam.setPublishDate(parse(cmd.getPublishDate(), tsf));
		exam.setAdmissionPrintEnd(parse(cmd.getAdmissionPrintEnd(), tsf));
		exam.setAdmissionPrintStart(parse(cmd.getAdmissionPrintStart(), tsf));
		exam.setApplyBeginDate(parse(cmd.getApplyBeginDate(), tsf));
		exam.setApplyDeadDate(parse(cmd.getApplyDeadDate(), tsf));
		exam.setExamDate(parse(cmd.getExamDate(), dsf));
		exam.setReApplyEnd(parse(cmd.getReApplyEnd(), tsf));
		exam.setReApplyStart(parse(cmd.getReApplyStart(), tsf));
		exam.setScorePublishDate(parse(cmd.getScorePublishDate(), tsf));
		exam.setRegFee(cmd.getRegFee() == null || cmd.getRegFee().length() == 0 ? Double
				.valueOf(0) : Double.valueOf(cmd.getRegFee()));
		exam.setOnlinePay(cmd.isOnlinePay());
		exam.setPayStartDate(parse(cmd.getPayStartDate(), tsf));
		exam.setPayEndDate(parse(cmd.getPayEndDate(), tsf));
		if (exam.getId() == null) {
			examDao.add(exam);
		} else {
			examDao.update(exam);
		}
	}

	@Override
	public void search(HttpServletRequest request, AnnouncementListCommand cmd)
			throws Exception {
		cmd.setEid(null);
		List<Exam> exams = examDao.search(cmd);
		cmd.setResult(exams);
	}

	@Transactional
	@Override
	public void delete(HttpServletRequest request, AnnouncementListCommand cmd,
			BindException errors) throws Exception {
		if (cmd.getEid() != null) {
			examDao.delete(examDao.get(cmd.getEid()));
		}
	}

	@Override
	@SuppressWarnings("rawtypes")
	public Map initExamSelect(AnnouncementListCommand cmd, Errors errors)
			throws Exception {
		cmd.setEid(null);
		cmd.setStatus(MasterUtil.getInstance().getMaster(10, "02").getId());
		List<Exam> exams = examDao.search(cmd);
		cmd.setResult(exams);
		return null;
	}

	@Override
	public Exam examSelect(HttpServletRequest request,
			HttpServletResponse response, AnnouncementListCommand cmd,
			BindException errors) throws Exception {
		if (cmd.getEid() != null) {
			return examDao.get(cmd.getEid());
		}
		return null;
	}

	/**
	 * @return the examDao
	 */
	public ExamDao getExamDao() {
		return examDao;
	}

	/**
	 * @param examDao
	 *            the examDao to set
	 */
	public void setExamDao(ExamDao examDao) {
		this.examDao = examDao;
	}

}
